package services;

import domain.entities.*;

import java.util.ArrayList;
import java.util.List;

public class CriteriaCenter extends Criteria {
    private Centro centro;
    private Specialty specialty;
    private TherapeuticArea therapeuticArea;
    private Address address;

    public CriteriaCenter(Centro centro) {
        this.centro = centro;
        this.specialty = getSpecialty(centro);
        this.therapeuticArea = getTherapeuticArea(centro);
        this.address = getAddress(centro);
    }

    @Override
    public boolean isIn(Object element){
        boolean isIn = true;

        Centro other = (Centro)element;

        isIn &= (specialty == null) || (hasSpecialty(other));
        isIn &= isSubstring(centro.getInstitucion(),other.getInstitucion());
        isIn &= (therapeuticArea == null) || (hasTherapeuticArea(other));
        isIn &= (address == null) || (address.getCountry() == null || address.getCountry().isEmpty()) || (hasCountry(other));
        isIn &= (address == null) || (address.getProvince() == null || address.getProvince().isEmpty()) || (hasRegion(other));

        return isIn;
    }

    private boolean hasCountry(Centro centro){
        for(CentroAddress ca : centro.getCentroAddresses()){
            if(ca!=null && ca.getAddress() != null && ca.getAddress().getCountry().equals(address.getCountry())){
                return true;
            }
        }
        return false;
    }

    private boolean hasRegion(Centro centro){
        for(CentroAddress ca : centro.getCentroAddresses()){
            if(ca!=null && ca.getAddress() != null && ca.getAddress().getProvince().equals(address.getProvince())){
                return true;
            }
        }
        return false;
    }

    private boolean hasSpecialty(Centro centro){
        for(CentroAddress ca : centro.getCentroAddresses()){
            if(ca!=null && ca.getSpeciality() != null && ca.getSpeciality().getId() != null && ca.getSpeciality().getId().equals(specialty.getId())){
                return true;
            }
        }
        return false;
    }

    private boolean hasTherapeuticArea(Centro centro){
        for(CentroAddress ca : centro.getCentroAddresses()){
            if(ca!=null && ca.getTherapeuticArea() != null && ca.getTherapeuticArea().getId() != null && ca.getTherapeuticArea().getId().equals(therapeuticArea.getId())){
                return true;
            }
        }
        return false;
    }

    private Specialty getSpecialty(Centro centro){
        CentroAddress ca = _getCentroAddress(centro);
        if (ca==null){
            return null;
        }else{
            return ca.getSpeciality();
        }
    }

    private TherapeuticArea getTherapeuticArea(Centro centro){
        CentroAddress ca = _getCentroAddress(centro);
        if (ca==null){
            return null;
        }else{
            return ca.getTherapeuticArea();
        }
    }
    private Address getAddress(Centro centro){
        CentroAddress ca = _getCentroAddress(centro);
        if (ca==null){
            return null;
        }else{
            return ca.getAddress();
        }
    }

    private CentroAddress _getCentroAddress(Centro centro){
        List<CentroAddress> cas = (ArrayList)centro.getCentroAddresses();
        return cas.get(0);
    }
}
